package com.lansi.fb.truck.controller;


import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lansi.bean.ResponseResult;
import com.lansi.fb.truck.bean.Truck;
import com.lansi.fb.truck.service.ITruckService;

@Controller
@RequestMapping("/chemical")
public class TruckController {
	//依赖注入
	@Resource
	private ITruckService truckService;
	
	//检测车牌号是否存在和卡的状态
	@RequestMapping("/checkTrucksn.do")
	@ResponseBody
	public ResponseResult<Void> checkTrucksn(/*String sn*/ @RequestBody Map<String,String> map){
		//1.创建rr对象		
		String sn=map.get("sn");
		 
		ResponseResult<Void> rr = new ResponseResult<Void>();
		System.out.println("检测车牌号是否存在:"+sn);	
		Truck truck =truckService.getTruckBySn(sn);
		if(truck!=null) {    //车牌号已存在
			String recyle = truck.getRecyle();
			Integer id = truck.getId();
			if("1".equals(recyle)) {  //发卡已收回--重新发卡
				Date date = new Date();
				truckService.updateTruck(id, null, null, null, null, null, null, date, null, "2", null);
				rr.setState(1);
				rr.setMessage("发卡成功");
			}else {
				rr.setState(-1);
				rr.setMessage("卡未收回");
			}
		}else {         //车牌号不存在
			rr.setState(2);
			rr.setMessage("车牌号可以使用");
		}
		return rr;
		
	}
	
	//处理车辆人员信息注册
	@RequestMapping("/registerTruck.do")
	@ResponseBody
	public ResponseResult<Void> RegisterPerson(
			/*String sn,
			String type,
			String driver,
			String drivercompany,
			String cardid,
			String memo*/
			@RequestBody Map<String,String> map
			){
		ResponseResult<Void> rr = new ResponseResult<Void>();		
		try {
			String sn =map.get("sn");
			String type=map.get("type");
			String driver=map.get("driver");
			String drivercompany=map.get("drivercompany");
			String cardid=map.get("cardid");
			String memo =map.get("memo");
			Truck truck1 =truckService.getTruckBySn(sn);
			if(truck1==null) {
				System.out.println("车辆信息注册:"+"sn:"+sn+" type:"+type+" driver:"+driver+" drivercompany:"+drivercompany+" cardid:"+cardid+" memo:"+memo);
				Truck truck = new Truck();
				truck.setSn(sn);
				truck.setType(type);
				truck.setDriver(driver);
				truck.setDrivercompany(drivercompany);
				truck.setCardid(cardid);
				truck.setCardassign(new Date());//发卡时间
				truck.setRecyle("2");
				truck.setMemo(memo);			
				truckService.register(truck);
				rr.setState(1);
				rr.setMessage("注册成功");
			}else {
				rr.setState(-1);
				rr.setMessage("注册失败/车牌号已存在");
			}
			
		} catch (RuntimeException e) {
			rr.setState(-1);
			rr.setMessage(e.getMessage());		
		}
		
		return rr;
			
		}

	//收回卡号
	@RequestMapping("/takeBackcard.do")
	@ResponseBody
	public ResponseResult<Void> takeBackcard(/*String sn*/@RequestBody Map<String,String> map){
		ResponseResult<Void> rr = new ResponseResult<Void>();
		try {
			String sn =map.get("sn");
			Truck truck =truckService.getTruckBySn(sn);
			if(truck!=null) { //卡号存在
				String recyle = truck.getRecyle();    //得到卡的状态
				Integer id = truck.getId();
				if("1".equals(recyle)) {  //发卡已收回
					rr.setState(1);
					rr.setMessage("卡已收回/收卡失败");
				}else {
					rr.setState(2);
					rr.setMessage("收卡成功");
					Date date = new Date();
					truckService.updateTruck(id, null, null, null, null, null, null, null, date, "1", null);
				}				
			}else {          //卡号不存在
				rr.setState(-1);
				rr.setMessage("卡号不存在");
			}			
		} catch (RuntimeException e) {
			rr.setState(-1);
			rr.setMessage(e.getMessage());
		}	
		return rr;
			
		}
	
	//处理删除车辆信息根据id
	@RequestMapping("/delTruck.do")
	@ResponseBody
	public ResponseResult<Void> delTruck(/*Integer id */@RequestBody Map<String,Integer> map){
		ResponseResult<Void> rr = new ResponseResult<Void>();
		try {
			Integer id = map.get("id");
			System.out.println("删除车辆的id:"+id);
			truckService.delTruckById(id);
			rr.setState(1);
			rr.setMessage("删除成功");
		} catch (RuntimeException e) {
			rr.setState(-1);
			rr.setMessage(e.getMessage());
		}
		return rr;
		
	}
	
	//修改用户信息
	@RequestMapping("/updateTruck.do")
	@ResponseBody
	public ResponseResult<Void> updateTruck(
			/*Integer id,
			String  sn,
			String  type,
			String  driver,
			String  drivercompany,
			String  cardid,
			String  memo*/	
			@RequestBody Map<String,Object> map
			){
		
		ResponseResult<Void> rr = new  ResponseResult<Void>();	
		try {	
				Integer id 		=(Integer) map.get("id");
				String sn 		=(String) map.get("sn");
				String type		=(String) map.get("type");
				String driver	=(String) map.get("driver");
				String drivercompany=(String) map.get("drivercompany");
				String cardid	=(String) map.get("cardid");
				String memo 	=(String) map.get("memo");
				System.out.println("车辆信息注册:"+"id:"+id+" sn:"+sn+" type:"+type+" driver:"+driver+" drivercompany:"+drivercompany+" cardid:"+cardid+" memo:"+memo);

				truckService.updateTruck(id, sn, type, null, driver, drivercompany, cardid, null, null, null, memo);
				rr.setState(1);
				rr.setMessage("修改成功");			
				
		} catch (RuntimeException e) {
			rr.setState(-1);
			rr.setMessage(e.getMessage());
		}
		return rr;
	}
	
	//查询出所有的用户信息
	@RequestMapping("/getTruck.do")
	@ResponseBody
	public ResponseResult<List<Truck>> getTruck(/*String sn,String cardid,Integer page*/	@RequestBody Map<String,Object> map){
		ResponseResult<List<Truck>> rr = new ResponseResult<List<Truck>>();
		try {
			String sn 		=(String) map.get("sn");
			String cardid	=(String) map.get("cardid");
			Integer page	=(Integer) map.get("page");
					
			if(page==null) {
				page=1;
			}
			Integer offset = (page-1)*10;//跳过的页数
			List<Truck> list = truckService.getTruck(sn, cardid, offset, 10);
			System.out.println("查询车辆结果:"+list);
			Integer Truckcount = truckService.getTruckcount(sn, cardid);
			System.out.println("查询到的数量:"+Truckcount);
			rr.setData(list);
			rr.setState(1);
			rr.setMessage(Truckcount.toString());
		} catch (RuntimeException e) {
			rr.setMessage(e.getMessage());
			rr.setState(-1);			
		}
		return rr;		
	}
}
